//
//  SubmitOrderViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "SubmitOrderViewController.h"
#import "SubmitFooter.h"
#import "AddressCell.h"
#import "SubmitGoodsCell.h"
#import "AddressManagerViewController.h"
#import "PayViewController.h"

@interface SubmitOrderViewController ()
@property (strong, nonatomic) IBOutlet UILabel *money;
@property (strong, nonatomic) IBOutlet UIView *toobar;
@property (strong, nonatomic) SubmitFooter *footer;

@end

@implementation SubmitOrderViewController

-  (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)initUI{
    self.title = LS(@"确认订单");
    self.footer = [SubmitFooter loadFromXIB];
    self.model.address = APP.user.address;
    self.money.text = [NSString stringWithFormat:LS(@"¥:%@"),self.model.price];
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[AddressCell nib] forCellReuseIdentifier:[AddressCell ID]];
    [self.tableView registerNib:[SubmitGoodsCell nib] forCellReuseIdentifier:[SubmitGoodsCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c: 0];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:10];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:-10];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.toobar a2:NSLayoutAttributeTop c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (APP.user.account ==  nil || APP.user.account.length <= 0) {
        return [Toast msg:LS(@"您还未登录，请先去登录")];
    }
    if (self.model.address == nil) {
        return [Toast msg:LS(@"请选择一个地址")];
    }
    [Toast startHud:LS(@"提交中..")];
    [GoodsTool delete:self.model complete:^{
        self.model.orderStatus = GoodsOrderStastusPayed;
        self.model.account = APP.user.account;
        self.model.orderID = [NSDate currentTimeStamp:@"yyyy/MM/dd HH:mm:ss"];
        [self.model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [Toast stopHud];
                PayViewController *vc = [PayViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            });
        }];
    }];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddressCell ID]];
        cell.cornerRadius = 10;
        cell.model = self.model.address;
        cell.img.image = [UIImage imageNamed:@"更多"];
        return cell;
    }else{
        SubmitGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:[SubmitGoodsCell ID]];
        if (cell.task) {
            [cell.task cancel];
        }
        cell.model = self.model;
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        AddressManagerViewController *vc = [AddressManagerViewController new];
        vc.selectAddress = self.model.address;
        WS(self)
        vc.block = ^(Address * _Nonnull address) {
            weakself.model.address = address;
            [tableView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == 0 ? 0 : 130;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section > 0){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 20, 130)];
        view.backgroundColor = [UIColor clearColor];
        [view addSubview:self.footer];
        self.footer.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *top = [self.footer a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:view a2:NSLayoutAttributeTop c:10];
        NSLayoutConstraint *left = [self.footer a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:view a2:NSLayoutAttributeLeft c:0];
        NSLayoutConstraint *right = [self.footer a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:view a2:NSLayoutAttributeRight c:0];
        NSLayoutConstraint *bottom = [self.footer a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:view a2:NSLayoutAttributeBottom c:0];
        [view addConstraints:@[top,left,right,bottom]];
        return view;
    }else{
        return nil;
    }
}

@end
